/*
============================================================================
 Name        : BlockChangeAward.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CBlockChangeAward implementation
============================================================================
*/

#include "BlockChangeAward.h"
#include "stringloader.h"
#include "eStockappui.h"
#include "BlockMemo.h"
#include "BlockQueryAward.h"
#include <eStock_L2hy.rsg>

CBlockChangeAward::CBlockChangeAward(CBaseBlock* bsBack,const TInt& aTotalPoint):
CBlock(bsBack),
m_nTotalPoints(aTotalPoint)
{
	if(m_pGlobal->g_AMonthNeedsPoints)
		m_nMonths = aTotalPoint/m_pGlobal->g_AMonthNeedsPoints;
}

CBlockChangeAward::~CBlockChangeAward()
{
	if(m_vText != NULL)
	{
		m_vText->Reset();
		delete m_vText;
	}
	delete m_selBox;
}

CBlockChangeAward* CBlockChangeAward::NewLC(CBaseBlock* bsBack,const TInt& aTotalPoint)
{
	CBlockChangeAward* self = new (ELeave)CBlockChangeAward(bsBack,aTotalPoint);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockChangeAward* CBlockChangeAward::NewL(CBaseBlock* bsBack,const TInt& aTotalPoint)
{
	CBlockChangeAward* self=CBlockChangeAward::NewLC(bsBack,aTotalPoint);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockChangeAward::ConstructL()
{
	CBlock::ConstructL();
	m_vText =  new(ELeave)CDesCArraySeg(5);
	m_bSocketed = ETrue;
	CGlobal::GetDesFromID(m_strTitle, LOC_BUF_STR_CHANGEAWARD);
	m_selBox = CSelectBox::NewL();
	for (TInt i = 1; i <= m_nMonths; ++i)
	{
		TBuf<2> temp;
		temp.AppendNum(i);
		m_selBox->AddItem(i, temp);
	}
	SetNoticeText();
}

void CBlockChangeAward::SetNoticeText()
{
	m_vText->Reset();

	HBufC* buf = StringLoader::LoadLC(LOC_BUF_STR_CHANGENOTICE);
	TBuf<100> temp;
	TInt months = m_selBox->GetCurValue();
	if(m_pGlobal->g_AMonthNeedsPoints)
		temp.Format(buf->Des(),m_nTotalPoints,m_nTotalPoints/m_pGlobal->g_AMonthNeedsPoints, months);
	CleanupStack::PopAndDestroy();
	CreateTipArray(temp,*m_vText);
}

void CBlockChangeAward::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockChangeAward::OnDraw(CFbsBitGc& g) const
{
	TBuf<20> buftemp;
	TInt nHeight = m_nLineHeight-1;
	TInt nY = m_rect.iTl.iY;

	g.UseFont(m_pGlobal->g_font);
	m_selBox->Draw(g);
//	g.SetBrushColor(m_pGlobal->g_rgbText);
//	g.SetPenColor(m_pGlobal->g_rgbText);
//	g.DrawRect(TRect(TPoint(0, nY), TSize(nHeight, nHeight)));
//	g.DrawRect(TRect(TPoint(m_nScreenWidth-nHeight, nY), TSize(nHeight, nHeight)));
//
//	g.SetBrushColor(m_pGlobal->g_rgbBackGround);
//	g.SetPenColor(m_pGlobal->g_rgbBackGround);
//	TPoint ptArr1[3] = {TPoint(nHeight/2-4, nY+nHeight/2),TPoint(nHeight-4, nY+2), TPoint( nHeight-4, nY+nHeight-2)};
//	g.DrawPolygon(ptArr1, 3);
//	TPoint ptArr2[3] = {TPoint(m_nScreenWidth-nHeight/2+4, nY+nHeight/2),TPoint(m_nScreenWidth-nHeight+4, nY+2), TPoint(m_nScreenWidth-nHeight+4, nY+nHeight-2)};
//	g.DrawPolygon(ptArr2,3);
//
//
//	g.SetBrushColor(m_pGlobal->g_rgbHL_BG);
//	g.SetPenColor(m_pGlobal->g_rgbHL_BG);
//	g.DrawRect(TRect(TPoint(m_nLineHeight, nY), TSize(m_nScreenWidth-nHeight*2-2, nHeight)));
//
//	g.SetPenColor(m_pGlobal->g_rgbText);
//	buftemp.Format(_L("%d"),m_nMonths);
//	g.DrawText(buftemp, TPoint(m_rect.iBr.iX/2, m_nLineHeight+m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));
	for(TInt i=0; i<m_vText->Count(); i++)
	{
		TInt nY = (i+2)*m_nLineHeight+m_pGlobal->g_nFontHeight+m_pGlobal->m_nSpaceUp;
		g.DrawText((*m_vText)[i],TPoint(0,nY));
	}
}

void CBlockChangeAward::OnMouseUp(const TPoint& aPt)
{
	TRect selBoxRect(0, m_rect.iTl.iY, m_nScreenWidth, m_rect.iTl.iY + m_nLineHeight - 1);
	if (selBoxRect.Contains(aPt))
	{
		if (aPt.iX < m_nLineHeight)
			OnKeyEvent(m_pGlobal->g_nKeyLeft);
		else if (aPt.iX > m_nScreenWidth - m_nLineHeight)
			OnKeyEvent(m_pGlobal->g_nKeyRight);
	}
}

void CBlockChangeAward::OnKeyEvent(const TInt& keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyOK/* || keyCode=='*'*/)
	{
		m_bSocketed = EFalse;
		StartSocket();
		return;
	}
	else if(keyCode==m_pGlobal->g_nKeyCancel)
	{
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->DelBlock();
		return;
	}
	else if(keyCode==m_pGlobal->g_nKeyDown)
	{
		iToolbar->SetFocus(ETrue);
		DrawNow();
		return;
	}
	else if (m_selBox->OnKeyDown(keyCode))
	{
		int oldmonths = m_selBox->GetCurValue();
		if (oldmonths != m_nMonths)
		{
			m_nMonths = oldmonths;
			SetNoticeText();
			DrawNow();
		}
		return;
	}
	CBlock::OnKeyEvent(keyCode);
//	if (keyCode==m_pGlobal->g_nKeyLeft || keyCode==m_pGlobal->g_nKeyRight)
//	{
//		int oldmonths = m_nMonths;
//		if (keyCode==m_pGlobal->g_nKeyLeft)
//		{
//			if(oldmonths > 1)
//				oldmonths --;
//		}
//		else
//		{
//			if(oldmonths < m_nTotalPoints/m_pGlobal->g_AMonthNeedsPoints)
//				oldmonths++;
//		}
//		if(oldmonths != m_nMonths)
//		{
//			m_nMonths = oldmonths;
//			SetNoticeText();
//			DrawNow();
//		}
//		return;
//	}
}

TBool CBlockChangeAward::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);
	TInt nError = CGlobal::ReadInt32L(ptr);
	HBufC* strError = CGlobal::ReadStringL(ptr);
	m_bSocketed = ETrue;
	m_bExit = ETrue; //\xB1\xBE\xC0\xE0\x{05bb}\xCA\xD5\x{04bb}\xB1\xE9\xCA\xFD\xBE\xDD
	if (nError==0)
	{
		CBlockQueryAward* blockQuery = (CBlockQueryAward*)m_pBlockBack;
		blockQuery->m_bSocketed = EFalse;
		GetContainer()->AddBlock(CBlockMemo::NewL(m_pBlockBack, 500, strError->Des()));
	}
	else
	{
		GetContainer()->AddBlock(CBlockMemo::NewL(m_pBlockBack, 100, strError->Des()));
	}

	delete strError;
	return ETrue;
}

void CBlockChangeAward::WriteData(TDes8& aBuf)
{
	TInt months = m_selBox->GetCurValue();
	CGlobal::WriteString(aBuf, m_pGlobal->m_strUserName);
	CGlobal::WriteInt32L(aBuf, months);
}

TInt CBlockChangeAward::GetDataLength()
{
	return CGlobal::GetWriteLength(m_pGlobal->m_strUserName) + 4;
}

TInt16 CBlockChangeAward::GetDataType()
{
	if (m_bSocketed)
		return RequestDataType_None;
	else
		return RequestDataType_ChangeAward;
}

void CBlockChangeAward::SizeChanged()
{
	CBlock::SizeChanged();
	iToolbar->SetSoftKey(CToolbar::EOK, CToolbar::EBack);
	m_selBox->SetRect(0, m_rect.iTl.iY, m_nScreenWidth, m_rect.iTl.iY + m_nLineHeight - 1);
}
